#!/bin/sh

# Tests a set of commands listed in the command_parser.input file
# and runs those commands through the rtpp control channel, comparing
# the results with the expected results contained in the
# command_parser.output file.

BASEDIR="`dirname "${0}"`/.."
. "${BASEDIR}/functions"

run_command_parser() {
  socket=${1}
  shift
  for extra_opts in "${@}"
  do
    RTPP_ARGS="-d dbug -b -m 23820 -M 23823 ${extra_opts}"
    if [ "${socket}" = "stdio:" ]
    then
      ${RTPPROXY} -f -s "${socket}" ${RTPP_ARGS} < $BASEDIR/command_parser/command_parser.input 2>command_parser.rlog || return 1
    else
      RTPP_SOCKFILE="${socket}" rtpproxy_start ${RTPP_ARGS} || return 1
      if ! ${RTPP_QUERY} -t 10 -b -s "${socket}" -S "${TOP_BUILDDIR}/python/sippy_lite" -i $BASEDIR/command_parser/command_parser.input
      then
        rtpproxy_stop TERM
        return 1
      fi
      rtpproxy_stop TERM || return 1
    fi
  done
}

RECORD_DIR="${BUILDDIR}"
for socket in ${RTPP_TEST_SOCKETS}
do
  run_command_parser "${socket}" "" "-P" "-r ${RECORD_DIR}" "-P -r ${RECORD_DIR}" > command_parser.rout
  report "wait for the rtproxy shutdown on ${socket}"
  ${DIFF} ${BASEDIR}/command_parser/command_parser.output command_parser.rout
  report "command_parser on ${socket}"
done
